<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>20条曲线的ECharts示例</title>
  <script src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script>
  <style>
    #main {
      width: 800px;
      height: 400px;
      margin: 0 auto;
    }
  </style>
</head>

<body>
  <div id="main"></div>
  <script>
    var chartDom = document.getElementById('main')
    var myChart = echarts.init(chartDom)

    // 生成随机数据
    function getRandomData () {
      return Array.from({ length: 12 }, () => Math.floor(Math.random() * 1000))
    }

    var option = {
      tooltip: {
        trigger: 'axis',
        formatter: function (params) {
          let tooltipContent = `<strong>${params[0].axisValue}</strong><br/>` // 显示 x 轴值

          const dataCount = params.length // 数据项数量
          const half = Math.ceil(dataCount / 2) // 计算中间值

          // 左侧列内容
          let leftColumn = ''
          for (let i = 0; i < half; i++) {
            if (i < dataCount) {
              leftColumn += `<div style="text-align: left;">` +
                `<span style="display:inline-block; width:10px; height:10px; background-color:${params[i].color}; border-radius:50%; margin-right:5px;"></span>` +
                `${params[i].seriesName}: ${params[i].data}</div>`
            }
          }

          // 右侧列内容
          let rightColumn = ''
          for (let i = half; i < dataCount; i++) {
            if (i < dataCount) {
              rightColumn += `<div style="text-align: left;">` +
                `<span style="display:inline-block; width:10px; height:10px; background-color:${params[i].color}; border-radius:50%; margin-right:5px;"></span>` +
                `${params[i].seriesName}: ${params[i].data}</div>`
            }
          }

          tooltipContent += `<div style="display: flex; justify-content: space-between;">` +
            `<div style="width: 50%; text-align: left;">${leftColumn}</div>` +
            `<div style="width: 50%; text-align: left;">${rightColumn}</div>` +
            `</div>`

          return tooltipContent
        },
        position: function (point, params, dom, rect, size) {
          var x = Math.min(size.viewSize[0] - dom.offsetWidth, point[0])
          var y = Math.min(size.viewSize[1] - dom.offsetHeight, point[1]) + 10 // 可用的 Y 偏移量
          return [x, y]
        }
      },
      legend: {
        data: Array.from({ length: 20 }, (_, i) => `曲线 ${i + 1}`), // 曲线名称
        align: 'left'
      },
      xAxis: {
        type: 'category',
        data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'] // X轴数据
      },
      yAxis: {
        type: 'value'
      },
      series: Array.from({ length: 20 }, (_, i) => ({
        name: `曲线 ${i + 1}`,
        data: getRandomData(), // 随机数据
        type: 'line',
        smooth: true,
        itemStyle: {
          color: `hsl(${Math.random() * 360}, 70%, 50%)` // 随机颜色
        },
        lineStyle: {
          width: 2
        },
        areaStyle: {
          color: 'rgba(0, 0, 0, 0.1)' // 填充色
        }
      }))
    }

    myChart.setOption(option);
  </script>
</body>

</html>